package com.baizhi.controller;


import com.baizhi.entity.Users;
import com.baizhi.service.UsersService;
import com.baizhi.util.JwtUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhengg
 * @since 2021-02-07
 */
@RestController
public class UsersController {
    @Autowired
    private UsersService us;
    @GetMapping("login")
    public Map<String,Object> login(String name, String pwd){
        Map<String,Object> results=new HashMap<>();
        //存放查询条件的。
        QueryWrapper<Users> queryWrapper=new QueryWrapper<>();
        //第一个参数是表里面的字段名
        queryWrapper.eq("username",name);
        queryWrapper.eq("password",pwd);
        List<Users> list = us.list(queryWrapper);
        if(list.size()>0){
            //生成token
            Users users = list.get(0);
            String token = JwtUtil.getToken(new HashMap() {{
                put("name", users.getUsername());
            }});
            results.put("state",0);
            results.put("msg","登陆成功");
            results.put("token",token);
            results.put("realName", users.getRealname());
        }else{
            results.put("state",1);
            results.put("msg","登陆失败");
        }
        return results;
    }
}
